package com.huahang.fashionpress.controller;

import com.huahang.fashionpress.form.FormNick;
import com.huahang.fashionpress.mapper.NickroleMapper;
import com.huahang.fashionpress.mapper.RoleMapper;
import com.huahang.fashionpress.model.Nick;
import com.huahang.fashionpress.model.NickRole;
import com.huahang.fashionpress.model.Role;
import com.huahang.fashionpress.service.NickcService;
import com.huahang.fashionpress.vo.NickVO;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/nick")
@Transactional
public class AdminNickController {
    @Resource
    NickcService nickcService;
    @Resource
    RoleMapper roleMapper;
    @Resource
    NickroleMapper nickroleMapper;
    //显示所有角色信息
    @RequestMapping(value = "/list")
    public ModelAndView linklistNick(Map<String,Object> map){
        List<NickVO> nicklist = nickcService.selectAllFNick();
        map.put("nicklist",nicklist);
        return new ModelAndView("admin/nick/list",map);
    }
    //根据角色Id查询角色
    @RequestMapping(value = "/selectBynickId/{nickId}")
    public ModelAndView SelectNickBynickId(@PathVariable int nickId, Map<String,Object> map){
        List<NickVO> nicklist =new ArrayList<>();
        NickVO formNick = nickcService.selectFNickBynickId(nickId);
        nicklist.add(formNick);
        map.put("nicklist",nicklist);
        return new ModelAndView("admin/nick/list",map);
    }
    //根据角色Id删除角色
    @RequestMapping(value = "/delete")
    public String deleteNickBynickId(int nickId, Model model){
        nickroleMapper.deleteBynickId(nickId);
        nickcService.deleteNick(nickId);
        model.addAttribute("msg", "删除角色成功！");
        model.addAttribute("url", "/fashionpress/nick/list");
        return "admin/common/success";
    }
    //跳转添加页面
    @RequestMapping(value = "/add")
    public ModelAndView linkadd(Map<String,Object> map){
        List<Role> roleList = nickcService.SelectAllrole();
        map.put("rolelist",roleList);
        return new ModelAndView("admin/nick/add",map);
    }
    //跳转修改页面，并把角色信息传递
    @RequestMapping(value = "/update",method = RequestMethod.GET)
    public ModelAndView linkupdate(Map<String,Object> map, int nickId, HttpServletResponse response){
        NickVO nickVO = nickcService.selectFNickBynickId(nickId);
        response.setContentType("text/plain; charset=UTF-8");
        map.put("formnick",nickVO);
        List<Role> roleList = nickcService.SelectAllrole();
        map.put("rolelist",roleList);
        return new ModelAndView("admin/nick/add",map);
    }
    //对角色进行添加或者修改
    @Transactional
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String addNick(FormNick formNick,Model model,String roles){
        String[] midrole = roles.split(",");
        if(formNick.getNickId()==null){
            Nick nick = new Nick();
            nick.setNickName(formNick.getNickName());
            int id= nickcService.addNick(nick);
            for(String role:midrole){
                NickRole nickRole = new NickRole();
                nickRole.setNickId(id);
                nickRole.setRoleId(Integer.valueOf(role));
                nickroleMapper.insert(nickRole);
            }
            model.addAttribute("msg", "添加角色成功！");
        }else {
            Nick nick = new Nick();
            nick.setNickId(formNick.getNickId());
            nick.setNickName(formNick.getNickName());
            nickroleMapper.deleteBynickId(formNick.getNickId());
            for(String role:midrole){
                NickRole nickRole = new NickRole();
                nickRole.setNickId(formNick.getNickId());
                nickRole.setRoleId(Integer.valueOf(role));
                nickroleMapper.insert(nickRole);
            }
            nickcService.updateNick(nick);
            model.addAttribute("msg", "修改角色信息成功！");
        }
        model.addAttribute("url", "/fashionpress/nick/list");
        return "admin/common/success";
    }
}
